package com.loukou.lkt.lkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loukou.lkt.lkt.dao.AllocationOrderDao;
import com.loukou.lkt.lkt.dao.AllocationOrderDetailDao;
import com.loukou.lkt.lkt.dao.GoodsCostDao;
import com.loukou.lkt.lkt.dao.GoodsDao;
import com.loukou.lkt.lkt.dao.PoPurchaseOrderDao;
import com.loukou.lkt.lkt.dao.PoPurchaseOrderDetailDao;
import com.loukou.lkt.lkt.dao.StockInOrderDao;
import com.loukou.lkt.lkt.dao.StockInOrderDetailDao;
import com.loukou.lkt.lkt.dao.WarehouseDao;
import com.loukou.lkt.lkt.entity.AllocationOrderDetail;
import com.loukou.lkt.lkt.entity.Goods;
import com.loukou.lkt.lkt.entity.GoodsCost;
import com.loukou.lkt.lkt.entity.PoPurchaseOrderDetail;
import com.loukou.lkt.lkt.entity.StockInOrder;
import com.loukou.lkt.lkt.entity.StockInOrderDetail;
import com.loukou.lkt.lkt.entity.Warehouse;
import com.loukou.lkt.lkt.enums.StockInOrderTypeEnum;
import com.loukou.lkt.lkt.enums.WmsInOrderTypeEnum;
import com.loukou.lkt.lkt.enums.WmsOutOrderTypeEnum;
import com.loukou.lkt.lkt.req.WmsInOrderCallbackDetailReq;
import com.loukou.lkt.lkt.req.WmsInOrderCallbackReq;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.StockInOrderService;

@Service
public class StockInOrderServiceImpl implements StockInOrderService{
	@Autowired
	private PoPurchaseOrderDao poPurchaseOrderDao;
	@Autowired
	private PoPurchaseOrderDetailDao poPurchaseOrderDetailDao;
	@Autowired
	private StockInOrderDao stockInOrderDao;
	@Autowired
	private StockInOrderDetailDao stockInOrderDetailDao;
	@Autowired
	private AllocationOrderDao allocationOrderDao;
	@Autowired
	private AllocationOrderDetailDao allocationOrderDetailDao;
	@Autowired
	private WarehouseDao warehouseDao;
	@Autowired
	private GoodsCostDao goodsCostDao;
	@Autowired
	private GoodsDao goodsDao;
	
	/**
	 * 添加入库单
	 */
	@Override
	@Transactional
	public RespDto<Integer> addStockInOrder(WmsInOrderCallbackReq req) {
		RespDto<Integer> resp = new RespDto<Integer>();
		if(req == null){
			resp.setCode(401);
			resp.setMsg("参数为空");
			return resp;
		}
		
		//获取仓库ID
		Warehouse warehouseMsg=warehouseDao.findByWarehouseCode(req.getWarehouseNo());
		
		int orderType=StockInOrderTypeEnum.TYPE_PURCHASE.getType();//默认采购
		if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_PURCHASE.getType()){//采购
			orderType=StockInOrderTypeEnum.TYPE_PURCHASE.getType();
		}else if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_ALLOCATION.getType()){//调拨
			orderType=StockInOrderTypeEnum.TYPE_AllOCATION.getType();
		}else if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_CHECK.getType()){//盘点
			orderType=StockInOrderTypeEnum.TYPE_CHECK.getType();
		}
		
		//添加入库单
		StockInOrder stockinorder = new StockInOrder();
		stockinorder.setOutOrdercode(req.getInOrderCode());
		stockinorder.setOrderType(orderType);
		stockinorder.setWarehouseId(warehouseMsg.getWarehouseId());
		stockinorder.setReceiveUserName(req.getReceiveUserName());
		stockinorder.setReceiveTime(req.getReceiveTime());
		stockinorder.setRemark(req.getRemark());
		StockInOrder savedStockInOrder=stockInOrderDao.save(stockinorder);
		int stockInOrderId=savedStockInOrder.getId();//获取入库单ID
		
		//添加入库明细
		for(WmsInOrderCallbackDetailReq inOrderDetail:req.getDetails()){
			StockInOrderDetail stockInOrderDetail=new StockInOrderDetail();
			if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_PURCHASE.getType()){//采购
				PoPurchaseOrderDetail ppOrderDetail=poPurchaseOrderDetailDao.findOne(Integer.parseInt(inOrderDetail.getOutbizId()));
				if(ppOrderDetail == null){
					throw new RuntimeException("采购单明细不存在");
				}
				
				stockInOrderDetail.setOrderId(stockInOrderId);
				stockInOrderDetail.setGoodsId(ppOrderDetail.getGoodsId());
				stockInOrderDetail.setIsGift(ppOrderDetail.getIsGift());
				stockInOrderDetail.setTaxPrice(ppOrderDetail.getTaxPrice());
				stockInOrderDetail.setOrderNumber(ppOrderDetail.getOrderNumber());
				stockInOrderDetail.setTaxAmount(ppOrderDetail.getTaxAmount());
				stockInOrderDetail.setReceiveNumber(inOrderDetail.getReceiveNumber());
				stockInOrderDetail.setGoodsName(ppOrderDetail.getGoodsName());
				stockInOrderDetail.setTaosku(ppOrderDetail.getTaosku());
				stockInOrderDetail.setBn(ppOrderDetail.getBn());
			}else if(req.getOrderType()==WmsInOrderTypeEnum.TYPE_ALLOCATION.getType()){//调拨
				AllocationOrderDetail aOrderDetail=allocationOrderDetailDao.findOne(Integer.parseInt(inOrderDetail.getOutbizId()));
				if(aOrderDetail == null){
					throw new RuntimeException("调拨单明细不存在");
				}
				stockInOrderDetail.setOrderId(stockInOrderId);
				stockInOrderDetail.setGoodsId(aOrderDetail.getInGoodsId());
				stockInOrderDetail.setIsGift(0);
				stockInOrderDetail.setTaxPrice(0);
				stockInOrderDetail.setOrderNumber(aOrderDetail.getPlanQuantity());
				stockInOrderDetail.setTaxAmount(0);
				stockInOrderDetail.setReceiveNumber(inOrderDetail.getReceiveNumber());
				stockInOrderDetail.setGoodsName(aOrderDetail.getInGoodsName());
				stockInOrderDetail.setTaosku(aOrderDetail.getInGoodsSku());
				stockInOrderDetail.setBn(aOrderDetail.getInGoodsBarcode());
			}else if(req.getOrderType()==WmsOutOrderTypeEnum.TYPE_CHECK.getType()){//盘亏出库
				//获取最新成本价
				GoodsCost goodsCostMsg=goodsCostDao.getGoodsCost(inOrderDetail.getGoodsId());
				double taxPrice=goodsCostMsg.getCostPrice();
				double taxAmount=goodsCostMsg.getCostPrice()*inOrderDetail.getReceiveNumber();
				//获取商品信息
				Goods goodsMsg=goodsDao.findOne(inOrderDetail.getGoodsId());
				
				stockInOrderDetail.setOrderId(stockInOrderId);
				stockInOrderDetail.setGoodsId(inOrderDetail.getGoodsId());
				stockInOrderDetail.setTaxPrice(taxPrice);
				stockInOrderDetail.setOrderNumber(inOrderDetail.getReceiveNumber());
				stockInOrderDetail.setTaxAmount(taxAmount);
				stockInOrderDetail.setReceiveNumber(inOrderDetail.getReceiveNumber());
				stockInOrderDetail.setGoodsName(goodsMsg.getGoodsName());
				stockInOrderDetail.setTaosku(goodsMsg.getSku());
				stockInOrderDetail.setBn(goodsMsg.getBarcode());
			}
			stockInOrderDetailDao.save(stockInOrderDetail);
		}
		resp.setMsg("添加成功");
		resp.setResult(stockInOrderId);
		return resp;
	}
}
